package stark.app.function.speechrec;

import android.text.TextUtils;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SPUtils;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * Author：daizhongbin on 2024/5/28
 * Email：1027154338@qq.com
 * Desc:
 */
public class SpeechRecRecordManager {

    private static SpeechRecRecordManager sInstance;
    private SPUtils sSpUtils = SPUtils.getInstance("speechRec");
    private String key_speech_rec_list = "key_speech_rec_list";
    private List<SpeechRecRecord> recRecords = new ArrayList<>();

    private SpeechRecRecordManager() {
        String json = sSpUtils.getString(key_speech_rec_list);
        if (TextUtils.isEmpty(json)) {
            return;
        }
        recRecords = GsonUtils.fromJson(json, new TypeToken<List<SpeechRecRecord>>() {
        }.getType());
        if (recRecords == null) {
            recRecords = new ArrayList<>();
        }
    }

    public static synchronized SpeechRecRecordManager getInstance() {
        if (sInstance == null) {
            sInstance = new SpeechRecRecordManager();
        }
        return sInstance;
    }

    private void save() {
        String json = GsonUtils.toJson(recRecords);
        sSpUtils.put(key_speech_rec_list, json);
    }

    public void addRecord(SpeechRecRecord record) {
        if (!recRecords.contains(record)) {
            recRecords.add(record);
            save();
        }
    }

    public void delRecord(SpeechRecRecord record) {
        if (recRecords.contains(record)) {
            FileUtils.delete(record.filePath);
            recRecords.remove(record);
            save();
        }
    }

    public List<SpeechRecRecord> getRecRecords() {
        return recRecords;
    }
}
